package com.lwf.thymeleafdemo1;

import org.springframework.boot.SpringApplication;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;
@Slf4j
@SpringBootApplication
public class Thymeleafdemo1Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Thymeleafdemo1Application.class, args);

        // 获取app的 访问地址
        Environment environment = context.getBean(Environment.class);
        //environment.getProperty("server.servlet.context-path") 应用的上下文路径，也可以称为项目路径
        String host = null;
        try {
            host = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
        String port = environment.getProperty("server.port");
        String path = environment.getProperty("server.servlet.context-path");
        if (StringUtils.isEmpty(path)) {
            path = "";
        }
        log.info("\n访问地址：http://{}:{}{}", host, port, path);
        System.out.println("\n访问地址：http://" + host + ":" + port + path);
        System.out.println(port);
    }

}
